angular.module('order-status',[])

.controller 'OrderStatusCtrl', ($scope,$http,$stateParams,$state,$ionicHistory,$ionicScrollDelegate,userData,$localStorage,showToast,$ionicPopup,orderInfo,$cordovaInAppBrowser,$rootScope) ->
    $scope.order = orderInfo.order
    console.log $scope.order
    $scope.orderId = $stateParams.orderid
    loadData=()->
        $http.get '/tradeapi/',params:{action:'getaddress',userid:userData.getToken(),tradeid:$scope.order.id}
        .success (resp) ->
            if resp.status == '0'
                console.log resp.body
                $scope.addrlist=resp.body
                $scope.vipAddress = {
                    receiver:resp.receiver
                    mobile:resp.mobile
                    address:resp.address
                }
                $scope.checkIndexAddress = $scope.addrlist[0].id
                $scope.currentAddressId = $scope.addrlist[0].id

    $scope.payWay = [
        {id:'0',text:'支付宝支付'}
        {id:'1',text:'银行转账'}
    ]
    $scope.options = {
        location: 'no'
        clearcache: 'no'
        toolbar: 'yes'
    }
    $scope.$on '$ionicView.enter',->
        $scope.checkIndexPay = 0
        loadData()
    _.extend $scope,
        currentPay:
            id:'0'
            text:undefined
        goBack: ->
#            $ionicHistory.goBack()
            $state.go 'tab.order-detail'
        choiceAddressVip: ->
            $scope.checkIndexAddressVip =true
            $scope.checkIndexAddress = undefined
            $scope.currentAddressId = -1
        choiceAddress:(item)->
            $scope.checkIndexAddress = item.id
            $scope.checkIndexAddressVip =false
            $scope.currentAddressId = item.id
        choicePayWay:(item)->
            $scope.checkIndexPay = item.id
            $scope.currentPay = item
        goConfirm: ->
            console.log $scope.currentPay.id
            if $scope.currentPay.id == '0'
                $http.post('/utf-8data/',{action:'copytrade',shopid:userData.getToken(),orderid:$scope.orderId,addressid:$scope.currentAddressId,paytype:$scope.currentPay.id})
                .success (resp) ->
                    if resp.status == '0'
                        showToast resp.msg
                        $cordovaInAppBrowser.open("http://www.upd365.com/onlinepay/mobilepayforshop/?userid=#{userData.getToken()}&tradeno=#{resp.tradeno}", '_blank', $scope.options)
                        .then (event) ->
                            logger.error 'event',JSON.stringify(event)
                        .catch (event)->
                            console.log
                        $ionicHistory.goBack(-2)
                    else
                        showToast resp.msg
                        $ionicHistory.goBack(-2)
                .error ->
                    showToast '您的网络又调皮了'
            else if $scope.currentPay.id == '1'
                $http.post('/utf-8data/',{action:'copytrade',shopid:userData.getToken(),orderid:$scope.orderId,addressid:$scope.currentAddressId,paytype:$scope.currentPay.id})
                .success (resp) ->
                    if resp.status == '0'
                        showToast resp.msg
                        $ionicHistory.goBack(-2)
                    else
                        showToast resp.msg
                        $ionicHistory.goBack(-2)
                .error ->
                    showToast '您的网络又调皮了'
        goAddressAdd:->
            $state.go 'tab.edit-address-orders',{origin:'order-status'}
            $localStorage.set 'addressInfo', {
                id:'0'
                reciveuser:''
                phonenumber:''
                address:''
                remark:''
            }
        goEdit:(item)->
            $state.go 'tab.edit-address'
            $localStorage.set 'addressInfo', {
                id:item.id
                reciveuser:item.reciveuser
                phonenumber:item.phonenumber
                address:item.address
                remark:item.remark
            }
    $rootScope.$on '$cordovaInAppBrowser:loadstop', (e, event) ->
        logger.error 'e:',_.keys(e)
        logger.error 'event:',_.keys(event)
